package com.fenxiangle.yueding.feature.mine.view.adapter;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;

import com.fenxiangle.yueding.R;
import com.fenxiangle.yueding.common.widget.dialog.ActionSheetDialog;
import com.fenxiangle.yueding.common.widget.dialog.OnOperItemClickL;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.T;

import butterknife.BindView;
import butterknife.OnClick;

public class Share2Activity extends BaseActivity implements OnOperItemClickL {

    @BindView(R.id.iv_share_wx)
    ImageView mIvShareWx;
    private ClipboardManager myClipboard;
    private ClipData myClip;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    private ActionSheetDialog dialog;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_share2;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initView() {
        initToolBar(mToolbar, "联系客服", true, true);

        final String[] stringItems = {"保存到本地"};
        dialog = new ActionSheetDialog(this, stringItems, null);
        dialog.isTitleShow(false);
        dialog.setOnOperItemClickL(this);
    }

    @Override
    protected void initData() {
        myClipboard = (ClipboardManager) this.getSystemService(CLIPBOARD_SERVICE);
    }


    @OnClick(R.id.btn_cpoy)
    public void onViewClicked() {
        myClip = ClipData.newPlainText("text", "juaner9876543210");
        myClipboard.setPrimaryClip(myClip);
        T.showShort("文本已复制！");
    }

    @Override
    protected void initEvent() {
        mIvShareWx.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (dialog != null) dialog.show();
                return false;
            }
        });
    }

    @Override
    public void onOperItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {
            case 0:
                saveImage(mIvShareWx);
                break;

        }

    }

    private void saveImage(ImageView imageView) {
        imageView.setDrawingCacheEnabled(true);//开启catch，开启之后才能获取ImageView中的bitmap
        Bitmap bitmap = imageView.getDrawingCache();//获取imageview中的图像
        MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "这是title", "这是description");
        showMsg("保存成功！");
        imageView.setDrawingCacheEnabled(false);//关闭catch
        if (dialog != null) dialog.dismiss();

    }
}
